/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "ssd2828.h"
//#include "R69434.h"
//#include "ST7796S__PH035NA_01B.h"
#include "ST7703__BV055HDE_N47_3Q00.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_NVIC_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

int _write(int file, char *ptr, int len)
{
  switch (file)
  {
  case STDOUT_FILENO: //stdout
    HAL_UART_Transmit(&huart1, (uint8_t *)ptr, len, 0xff);
    break;
  case STDERR_FILENO: // stderr
    // Send the string somewhere
    break;
  default:
    return -1;
  }
  return len;
}

void init(void)
{
  //SSD2828_Initial
				        W_C(0xb7);
				        W_D(0x50);//50=TX_CLK 70=PCLK
				        W_D(0x00);   //Configuration Register

				        W_C(0xb8);
				        W_D(0x00);
				        W_D(0x00);   //VC(Virtual ChannelID) Control Register

				        W_C(0xb9);
				        W_D(0x00);//1=PLL disable
				        W_D(0x00);
	                               //TX_CLK/MS should be between 5Mhz to100Mhz
				        W_C(0xBA);//PLL=(TX_CLK/MS)*NS 8228=480M 4428=240M  061E=120M 4214=240M 821E=360M 8219=300M
				        W_D(0x14);//D7-0=NS(0x01 : NS=1)
				        W_D(0x42);//D15-14=PLL范围 00=62.5-125 01=126-250 10=251-500 11=501-1000  DB12-8=MS(01:MS=1)

				        W_C(0xBB);//LP Clock Divider LP clock = 400MHz / LPD / 8 = 240 / 8 / 4 = 7.5MHz
				        W_D(0x03);//D5-0=LPD=0x1 – Divide by 2
				        W_D(0x00);

				        W_C(0xb9);
				       	W_D(0x01);//1=PLL disable
				       	W_D(0x00);
				        //MIPI lane configuration
				        W_C(0xDE);//通道数
				        W_D(0x00);//11=4LANE 10=3LANE 01=2LANE 00=1LANE
				        W_D(0x00);

				        W_C(0xc9);
				        W_D(0x02);
				        W_D(0x23);   //p1: HS-Data-zero  p2: HS-Data- prepare  --> 8031 issue
				       // delay_ms(100);


/*============================================================================*/

	//LCD driver initialization
	W_C(0xB7);
	W_D(0x50);//10=TX_CLK 30=PCLK
	W_D(0x02);

	W_C(0xBD);
	W_D(0x00);
	W_D(0x00);



//25




//------------------------------------------------------
// LCD Driver : ILI9881C Initial(TM034XDZP03-00)
//------------------------------------------------------
GP_COMMAD_PA(4);W_D(0xFF);W_D(0x98);W_D(0x81);W_D(0x03);
//GIP
GP_COMMAD_PA(2);W_D(0x01);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x02);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x03);W_D(0x73);
GP_COMMAD_PA(2);W_D(0x04);W_D(0xD3);
GP_COMMAD_PA(2);W_D(0x05);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x06);W_D(0x0A);
GP_COMMAD_PA(2);W_D(0x07);W_D(0x0F);
GP_COMMAD_PA(2);W_D(0x08);W_D(0x00);

GP_COMMAD_PA(2);W_D(0x09);W_D(0x01);
GP_COMMAD_PA(2);W_D(0x0A);W_D(0x01);
GP_COMMAD_PA(2);W_D(0x0B);W_D(0x01);
GP_COMMAD_PA(2);W_D(0x0C);W_D(0x01);
GP_COMMAD_PA(2);W_D(0x0D);W_D(0x01);
GP_COMMAD_PA(2);W_D(0x0E);W_D(0x01);

GP_COMMAD_PA(2);W_D(0x0F);W_D(0x19);	//47% ????CLK duty?С
GP_COMMAD_PA(2);W_D(0x10);W_D(0x19);

GP_COMMAD_PA(2);W_D(0x11);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x12);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x13);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x14);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x15);W_D(0x00);
                    
GP_COMMAD_PA(2);W_D(0x16);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x17);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x18);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x19);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x1A);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x1B);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x1C);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x1D);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x1E);W_D(0x40);
GP_COMMAD_PA(2);W_D(0x1F);W_D(0x80);
GP_COMMAD_PA(2);W_D(0x20);W_D(0x06);
GP_COMMAD_PA(2);W_D(0x21);W_D(0x01);
GP_COMMAD_PA(2);W_D(0x22);W_D(0x00);//GS_PANEL=0 TOP-BOTTOM   Forward
                    
GP_COMMAD_PA(2);W_D(0x23);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x24);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x25);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x26);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x27);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x28);W_D(0x33);
GP_COMMAD_PA(2);W_D(0x29);W_D(0x03);
GP_COMMAD_PA(2);W_D(0x2A);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x2B);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x2C);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x2D);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x2E);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x2F);W_D(0x00);
                    
GP_COMMAD_PA(2);W_D(0x30);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x31);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x32);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x33);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x34);W_D(0x03);
GP_COMMAD_PA(2);W_D(0x35);W_D(0x00);

GP_COMMAD_PA(2);W_D(0x36);W_D(0x03);

GP_COMMAD_PA(2);W_D(0x37);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x38);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x39);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x3A);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x3B);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x3C);W_D(0x00);
                    
GP_COMMAD_PA(2);W_D(0x3D);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x3E);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x3F);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x40);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x41);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x42);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x43);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x44);W_D(0x00);

//----------GIP_2--------------
GP_COMMAD_PA(2);W_D(0x50);W_D(0x01);
GP_COMMAD_PA(2);W_D(0x51);W_D(0x23);
GP_COMMAD_PA(2);W_D(0x52);W_D(0x45);
GP_COMMAD_PA(2);W_D(0x53);W_D(0x5D);
GP_COMMAD_PA(2);W_D(0x54);W_D(0x89);
GP_COMMAD_PA(2);W_D(0x55);W_D(0xAB);
GP_COMMAD_PA(2);W_D(0x56);W_D(0x01);
GP_COMMAD_PA(2);W_D(0x57);W_D(0x23);
GP_COMMAD_PA(2);W_D(0x58);W_D(0x45);
GP_COMMAD_PA(2);W_D(0x59);W_D(0x67);
GP_COMMAD_PA(2);W_D(0x5A);W_D(0x89);
GP_COMMAD_PA(2);W_D(0x5B);W_D(0xAB);
GP_COMMAD_PA(2);W_D(0x5C);W_D(0xCD);
GP_COMMAD_PA(2);W_D(0x5D);W_D(0xEF);

GP_COMMAD_PA(2);W_D(0x5E);W_D(0x11);	//01
GP_COMMAD_PA(2);W_D(0x5F);W_D(0x08);
GP_COMMAD_PA(2);W_D(0x60);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x61);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x62);W_D(0x01);
GP_COMMAD_PA(2);W_D(0x63);W_D(0x0D);
GP_COMMAD_PA(2);W_D(0x64);W_D(0x0C);
GP_COMMAD_PA(2);W_D(0x65);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x66);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x67);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x68);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x69);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x6A);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x6B);W_D(0x0F);
GP_COMMAD_PA(2);W_D(0x6C);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x6D);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x6E);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x6F);W_D(0x02);

GP_COMMAD_PA(2);W_D(0x70);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x71);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x72);W_D(0x0E);
GP_COMMAD_PA(2);W_D(0x73);W_D(0x06);
GP_COMMAD_PA(2);W_D(0x74);W_D(0x07);
GP_COMMAD_PA(2);W_D(0x75);W_D(0x08);
GP_COMMAD_PA(2);W_D(0x76);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x77);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x78);W_D(0x01);
GP_COMMAD_PA(2);W_D(0x79);W_D(0x0D);
GP_COMMAD_PA(2);W_D(0x7A);W_D(0x0C);
GP_COMMAD_PA(2);W_D(0x7B);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x7C);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x7D);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x7E);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x7F);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x80);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x81);W_D(0x0F);
GP_COMMAD_PA(2);W_D(0x82);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x83);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x84);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x85);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x86);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x87);W_D(0x02);
GP_COMMAD_PA(2);W_D(0x88);W_D(0x0E);
GP_COMMAD_PA(2);W_D(0x89);W_D(0x06);
GP_COMMAD_PA(2);W_D(0x8A);W_D(0x07);

GP_COMMAD_PA(4);W_D(0xFF);W_D(0x98);W_D(0x81);W_D(0x01);
GP_COMMAD_PA(2);W_D(0x2E);W_D(0x50);		//Gate number 800
    
GP_COMMAD_PA(4);W_D(0xFF);W_D(0x98);W_D(0x81);W_D(0x04);
GP_COMMAD_PA(2);W_D(0x6C);W_D(0x15);    //Set VCORE voltage =1.5V
GP_COMMAD_PA(2);W_D(0x6E);W_D(0x2A);      //di_pwr_reg=0 for power mode 2A //VGH clamp 12V
GP_COMMAD_PA(2);W_D(0x6F);W_D(0x33);     // reg vcl + pumping ratio VGH=3x VGL=-2.5x
GP_COMMAD_PA(2);W_D(0x8D);W_D(0x15);       //VGL clamp -10V //0x15  ***
GP_COMMAD_PA(2);W_D(0x3A);W_D(0xA4);      //POWER SAVING
GP_COMMAD_PA(2);W_D(0x87);W_D(0xBA);       //ESD
    
GP_COMMAD_PA(2);W_D(0x26);W_D(0x76);       //ESD       
GP_COMMAD_PA(2);W_D(0xB2);W_D(0xD1);        //ESD 
GP_COMMAD_PA(2);W_D(0x00);W_D(0x80);    //2 lane

GP_COMMAD_PA(4);W_D(0xFF);W_D(0x98);W_D(0x81);W_D(0x01);
GP_COMMAD_PA(2);W_D(0x22);W_D(0x0A);     //BGR);W_D( SS
GP_COMMAD_PA(2);W_D(0x31);W_D(0x00);      //column inversion
GP_COMMAD_PA(2);W_D(0x53);W_D(0x5d);       //VCOM1 //6a
GP_COMMAD_PA(2);W_D(0x55);W_D(0x61);       //VCOM2 //6a
GP_COMMAD_PA(2);W_D(0x50);W_D(0x9F);       //VREG1OUT   //B7=4.9 AF=4.8 A6=4.7 9f=4.6 (98=4.526V)  8E=4.4     
GP_COMMAD_PA(2);W_D(0x51);W_D(0x9A);       //VREG1OUT 
GP_COMMAD_PA(2);W_D(0x60);W_D(0x0C);       //SDT
                    
GP_COMMAD_PA(2);W_D(0xA0);W_D(0x00);         //VP255	Gamma P
GP_COMMAD_PA(2);W_D(0xA1);W_D(0x22);         //VP251  
GP_COMMAD_PA(2);W_D(0xA2);W_D(0x33);         //VP247 
GP_COMMAD_PA(2);W_D(0xA3);W_D(0x13);         //VP243   
GP_COMMAD_PA(2);W_D(0xA4);W_D(0x17);         //VP239     -
GP_COMMAD_PA(2);W_D(0xA5);W_D(0x2B);         //VP231   
GP_COMMAD_PA(2);W_D(0xA6);W_D(0x1F);         //VP219 
GP_COMMAD_PA(2);W_D(0xA7);W_D(0x1F);         //VP203  
GP_COMMAD_PA(2);W_D(0xA8);W_D(0xAD);         //VP175
GP_COMMAD_PA(2);W_D(0xA9);W_D(0x1E);         //VP144
GP_COMMAD_PA(2);W_D(0xAA);W_D(0x29);         //VP111
GP_COMMAD_PA(2);W_D(0xAB);W_D(0x9D);         //VP80 
GP_COMMAD_PA(2);W_D(0xAC);W_D(0x1D);         //VP52 
GP_COMMAD_PA(2);W_D(0xAD);W_D(0x1B);         //VP36
GP_COMMAD_PA(2);W_D(0xAE);W_D(0x4F);         //VP24
GP_COMMAD_PA(2);W_D(0xAF);W_D(0x22);         //VP16
GP_COMMAD_PA(2);W_D(0xB0);W_D(0x2A);         //VP12
GP_COMMAD_PA(2);W_D(0xB1);W_D(0x5A);         //VP8 
GP_COMMAD_PA(2);W_D(0xB2);W_D(0x67);         //VP4
GP_COMMAD_PA(2);W_D(0xB3);W_D(0x3F);

GP_COMMAD_PA(2);W_D(0xB6);W_D(0xa8);          //default commented****************
GP_COMMAD_PA(2);W_D(0xB7);W_D(0x03);          //2lane);W_D(??????


GP_COMMAD_PA(2);W_D(0xC0);W_D(0x00);
GP_COMMAD_PA(2);W_D(0xC1);W_D(0x22);
GP_COMMAD_PA(2);W_D(0xC2);W_D(0x33);
GP_COMMAD_PA(2);W_D(0xC3);W_D(0x13);
GP_COMMAD_PA(2);W_D(0xC4);W_D(0x17);
GP_COMMAD_PA(2);W_D(0xC5);W_D(0x2B);
GP_COMMAD_PA(2);W_D(0xC6);W_D(0x1F);
GP_COMMAD_PA(2);W_D(0xC7);W_D(0x1F);
GP_COMMAD_PA(2);W_D(0xC8);W_D(0xAD);
GP_COMMAD_PA(2);W_D(0xC9);W_D(0x1E);
GP_COMMAD_PA(2);W_D(0xCA);W_D(0x29);
GP_COMMAD_PA(2);W_D(0xCB);W_D(0x9D);
GP_COMMAD_PA(2);W_D(0xCC);W_D(0x1D);
GP_COMMAD_PA(2);W_D(0xCD);W_D(0x1B);
GP_COMMAD_PA(2);W_D(0xCE);W_D(0x4F);
GP_COMMAD_PA(2);W_D(0xCF);W_D(0x22);
GP_COMMAD_PA(2);W_D(0xD0);W_D(0x2A);
GP_COMMAD_PA(2);W_D(0xD1);W_D(0x5A);
GP_COMMAD_PA(2);W_D(0xD2);W_D(0x67);
GP_COMMAD_PA(2);W_D(0xD3);W_D(0x3F);

GP_COMMAD_PA(2);W_D(0x2d);W_D(0xff);//bist
GP_COMMAD_PA(2);W_D(0x2f);W_D(0x71);//bist

GP_COMMAD_PA(4);W_D(0xFF);W_D(0x98);W_D(0x81);W_D(0x00);
GP_COMMAD_PA(2);W_D(0x36);W_D(0x00);   //RGB?????
//GP_COMMAD_PA(2);0x36);W_D(0x03);   //RGB?????
//GP_COMMAD_PA(2);0x51);W_D(0xFF);
//GP_COMMAD_PA(2);0x53);W_D(0x2C);
//LCD_Gen_write_1A_0P(0x35);
GP_COMMAD_PA(1);W_D(0x35);
GP_COMMAD_PA(1);W_D(0x11);//????
delay_ms(120); 
GP_COMMAD_PA(1);W_D(0x29);//?????
delay_ms(10); 








		


////////////////////////////////////////////////////////////////////////////////////////////////


			    				    	    				    	               W_C(0xb7);
			    			    				    	    				    	W_D(0x50);
			    			    				    	    				    	W_D(0x02);

			    			    				    	    				    	W_C(0xBD);
			    			    				    	    				    	W_D(0x00);
			    			    				    	    				    	W_D(0x00);



			    				    	    				     	W_C(0xBC);
			    				    	    				    	W_D(0x00);
			    				    	    				    	W_D(0x00);

			    				    	    				    	//W_C(0xBF);
			    				    	    				    	W_C(0x11);     //
			    				    	    				    	//W_D(0x00);     //
			    				    	    				    	delay_ms(100);

			    				    	    				    	W_C(0xBC);
			    				    	    				    	W_D(0x00);
			    				    	    				    	W_D(0x00);
			    				    	    				    	//W_C(0xBF);
			    				    	    				    	W_C(0x29);    //  Display On
			    				    	    				    	delay_ms(200);








	//SSD2825_Initial
	W_C(0xb7);
	W_D(0x50);
	W_D(0x00);   //Configuration Register

	W_C(0xb8);
	W_D(0x00);
	W_D(0x00);   //VC(Virtual ChannelID) Control Register

	W_C(0xb9);
	W_D(0x00);//1=PLL disable
	W_D(0x00);

	W_C(0xBA);//PLL=(TX_CLK/MS)*NS 8228=480M 4428=240M  061E=120M 4214=240M 821E=360M 8219=300M 8225=444M 8224=432
	W_D(0x2d);//D7-0=NS(0x01 : NS=1)
	W_D(0xc2);//D15-14=PLL范围 00=62.5-125 01=126-250 10=251-500 11=501-1000  DB12-8=MS(01:MS=1)

	W_C(0xBB);//LP Clock Divider LP clock = 400MHz / LPD / 8 = 480 / 8/ 8 = 7MHz
	W_D(7);//D5-0=LPD=0x1 – Divide by 2
	W_D(0x00);

	W_C(0xb9);
	W_D(0x01);//1=PLL disable
	W_D(0x00);

	W_C(0xc9);
	W_D(0x02);
	W_D(0x23);   //p1: HS-Data-zero  p2: HS-Data- prepare  --> 8031 issue
	delay_ms(100);

	W_C(0xCA);
	W_D(0x01);//CLK Prepare
	W_D(0x23);//Clk Zero

	W_C(0xCB); //local_write_reg(addr=0xCB,data=0x0510)
	W_D(0x10); //Clk Post
	W_D(0x05); //Clk Per

	W_C(0xCC); //local_write_reg(addr=0xCC,data=0x100A)
	W_D(0x05); //HS Trail
	W_D(0x10); //Clk Trail

	W_C(0xD0);
	W_D(0x00);
	W_D(0x00);

	//RGB interface configuration
	W_C(0xB1);
	W_D(HSPW);//HSPW 07
	W_D(VSPW);//VSPW 05

	W_C(0xB2);
	W_D(HBPD);//HBPD 0x64=100
	W_D(VBPD);//VBPD 8 减小下移

	W_C(0xB3);
	W_D(HFPD);//HFPD 8
	W_D(VFPD);//VFPD 10

	W_C(0xB4);//Horizontal active period 720=02D0
	W_D(0x20);//013F=319 02D0=720
	W_D(0x03);

	W_C(0xB5);//Vertical active period 1280=0500
	W_D(0x20);//01DF=479 0500=1280
	W_D(0x03);

	
	W_C(0xB6);//RGB CLK  16BPP=00 18BPP=01
	//W_D(0x07);//D7=0 D6=0 D5=0  D1-0=11 – 24bpp
	W_D(0x03);//D7=0 D6=0 D5=0  D1-0=11 – 24bpp
	W_D(0x20);//D15=VS D14=HS D13=CLK D12-9=NC D8=0=Video with blanking packet. 00-F0



	//MIPI lane configuration
	W_C(0xDE);//通道数
	W_D(0x01);//11=4LANE 10=3LANE 01=2LANE 00=1LANE
	W_D(0x00);

	W_C(0xD6);//  05=BGR  04=RGB
	W_D(0x05);//D0=0=RGB 1:BGR D1=1=Most significant byte sent first
	W_D(0x00);


	W_C(0xB7);
	W_D(0x4B);
	W_D(0x02);

	//W_C(0x2C);
	//W_C(0x3C);
}

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();

  /* Initialize interrupts */
  MX_NVIC_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  printf("Program Start\r\n");

  // test spi
  /*spi_init();
  while(1)
  {
    spi_write(SPI_CMD, 0xa5);
    HAL_Delay(10);
    spi_write(SPI_DAT, 0x5a);
    HAL_Delay(10);
  }*/

  spi_init();

  ssd2828_hwreset();

  /*ssd2828_writeReg(0xd4, 0x00, 0xfa);

  uint16_t ssd2828ID = ssd2828_readReg(0xb0);
  
  printf("read 2828 id = 0x%x\r\n", ssd2828ID);*/

  /*for(uint8_t i = 0xB0; i < 0xff; i++)
  {
    uint16_t regData = ssd2828_readReg(i);

    printf("reg(0x%02x) = 0x%04x \r\n", i, regData);
  }*/
  //readMipi()

  //Init_SSD2828(init_ST7703__BV055HDE_N47_3Q00);
  //Init_SSD2828(init_ST7703_BV055HDE_47_IPS_Code);
  init();

  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    /*HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
    HAL_Delay(1000);
    HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
    HAL_Delay(1000);*/

    //uint8_t dat = 0xa5;
    //HAL_UART_Transmit(&huart1, &dat, 1, 100);
    //HAL_Delay(1000);

    //printf("hello stm32\r\n");
    //HAL_Delay(1000);

    /*HAL_GPIO_TogglePin(LCD_RST_GPIO_Port, LCD_RST_Pin);
    HAL_GPIO_TogglePin(LCD_CS_GPIO_Port, LCD_CS_Pin);
    HAL_GPIO_TogglePin(LCD_MOSI_GPIO_Port, LCD_MOSI_Pin);
    HAL_GPIO_TogglePin(LCD_SCK_GPIO_Port, LCD_SCK_Pin);
    HAL_Delay(1000);*/

    /*if(HAL_GPIO_ReadPin(LCD_MISO_GPIO_Port, LCD_MISO_Pin) == GPIO_PIN_SET) 
    {
      HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
    } else {
      HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
    }
    HAL_Delay(200);*/

    printf("Loop...\r\n");
    HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
    HAL_Delay(1000);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief NVIC Configuration.
  * @retval None
  */
static void MX_NVIC_Init(void)
{
  /* USART1_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(USART1_IRQn);
}

/**
  * @brief USART1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */
}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, LCD_RST_Pin | LCD_CS_Pin | LCD_SCK_Pin | LCD_MOSI_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, SHUT_Pin | U_CON_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin : LED_Pin */
  GPIO_InitStruct.Pin = LED_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : LCD_RST_Pin LCD_CS_Pin LCD_SCK_Pin LCD_MOSI_Pin */
  GPIO_InitStruct.Pin = LCD_RST_Pin | LCD_CS_Pin | LCD_SCK_Pin | LCD_MOSI_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pin : LCD_MISO_Pin */
  GPIO_InitStruct.Pin = LCD_MISO_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(LCD_MISO_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : SHUT_Pin U_CON_Pin */
  GPIO_InitStruct.Pin = SHUT_Pin | U_CON_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
